package wifiunion.com.notice.utils.imageprocess.utils;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 
 * @ClassName FileUtils 
 * @Description TODO 
 * @Version 
 * @Author wifiunion
 * @Creation 2016年6月20日 下午4:00:53 
 * @Mender wifiunion
 * @Modification 2016年6月20日 下午4:00:53 
*
 */
public class FileUtils {


    private final static String PATTERN = "yyyyMMddHHmmss";

    //记录文件日志


    public static File createTmpFile(Context context, String filePath) {

        String timeStamp = new SimpleDateFormat(PATTERN, Locale.CHINA).format(new Date());

        String externalStorageState = Environment.getExternalStorageState();

        File dir = new File(Environment.getExternalStorageDirectory() + filePath);

        if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
            if (!dir.exists()) {
                dir.mkdirs();
            }
            return new File(dir, timeStamp + ".jpg");
        } else {
            File cacheDir = context.getCacheDir();
            return new File(cacheDir, timeStamp + ".jpg");
        }

    }


    public static void createFile(String filePath) {
        String externalStorageState = Environment.getExternalStorageState();

        File dir = new File(Environment.getExternalStorageDirectory() + filePath);
        File cropFile = new File(Environment.getExternalStorageDirectory() + filePath + "/crop");

        if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
            if (!cropFile.exists()) {
                cropFile.mkdirs();
            }

            File file = new File(dir, ".nomedia");
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }




    /**
     * 追加文件：使用FileWriter
     *
     * @param fileName
     * @param content
     */
    public static void socketLog(String content) {

        File dirFile  = new File(Environment.getExternalStorageDirectory() + File.separator + "/intelcamera/log/socket");
        if(!dirFile.exists())
        {
            dirFile.mkdirs();
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd", Locale.CHINA).format(new Date());

        String exceptionFile =  Environment.getExternalStorageDirectory() + File.separator + "/intelcamera/log/socket" + File.separator + timeStamp + ".txt";


        File file = new File(exceptionFile);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        FileWriter writer = null;
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(exceptionFile, true);
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(writer != null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }




}